package org.openkanban.client.ui.presenter;

import org.openkanban.client.util.RootPanel;
import org.openkanban.shared.dto.BoardDto;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;

/**
 * Callback that handles get default board request
 */
public class GetDefaultBoardByUserIdRequestCallback implements RequestCallback {

    @Override
    public void onError(Request request, Throwable exception) {

    }

    @Override
    public void onResponseReceived(Request request, Response response) {
	BoardDto boardDto = new BoardDto(response.getText());
	runWhiteBoardInitialization(boardDto);
    }

    private void runWhiteBoardInitialization(BoardDto boardDto) {
	if (boardIsNew(boardDto)) {
	    RootPanel.activateBoardEditMode(true);
	} else {
	    RootPanel.main.getBoard().getWhiteBoardView().setBoardDto(boardDto);
	    RootPanel.activateBoardEditMode(false);
	}
    }

    private boolean boardIsNew(BoardDto boardDto) {
	return boardDto.getBoardId() == null;
    }

}
